package com.zh.harmony.handlerthread.sample.slice;

import com.zh.harmony.handlerthread.HandlerThread;
import com.zh.harmony.handlerthread.ResourceTable;
import com.zh.harmony.handlerthread.sample.Constant;
import com.zh.harmony.handlerthread.sample.service.DownloadService;
import com.zh.harmony.handlerthread.sample.util.ToastUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Component;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.InnerEvent;

import java.util.ArrayList;

public class MainAbilitySlice extends AbilitySlice {
    private HandlerThread mHandlerThread;
    private EventHandler mEventHandler;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        mHandlerThread = new HandlerThread("handler-thread");
        mHandlerThread.start();
        mEventHandler = new EventHandler(mHandlerThread.getEventRunner()) {
            @Override
            protected void processEvent(InnerEvent event) {
                super.processEvent(event);
                if (event.eventId == 10086) {
                    //该方法在HandlerThread的线程中回调
                    String currentThreadName = Thread.currentThread().getName();
                    ToastUtil.toast(getContext(), currentThreadName);
                }
            }
        };
        //HandlerThread
        findComponentById(ResourceTable.Id_send_msg_to_handler).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                //主线程发消息到HandlerThread中处理
                InnerEvent innerEvent = InnerEvent.get();
                innerEvent.eventId = 10086;
                mEventHandler.sendEvent(innerEvent);
            }
        });
        //IntentService
        findComponentById(ResourceTable.Id_intent_service_download_image).setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                ArrayList<String> urls = new ArrayList<>();
                urls.add("https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1565684573&di=82491d3ea2a4d5195d6f8bd90eba1953&src=http://image.coolapk.com/picture/2016/1210/459462_1481302685_5118.png.m.jpg");
                urls.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fbpic.588ku.com%2Felement_origin_min_pic%2F17%2F06%2F13%2F5c5a1442f0ec72e59829ee10d891f224.jpg%21r650&refer=http%3A%2F%2Fbpic.588ku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1630825792&t=ee2d858a52a95b4e763d18e42840d734");
                urls.add("https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fattach.bbs.miui.com%2Fforum%2F201104%2F16%2F2136492e16kpc6oqcz1rie.jpg&refer=http%3A%2F%2Fattach.bbs.miui.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1630825793&t=b0afae877349fb9a2a81820afe2728a3");
                Operation operation = new Intent.OperationBuilder()
                        //设备Id，本机则是空字符串
                        .withDeviceId("")
                        //包名
                        .withBundleName(getBundleName())
                        //服务类名
                        .withAbilityName(DownloadService.class.getName())
                        .build();
                Intent intent = new Intent();
                //传递多个文件URL地址
                intent.setParam(Constant.URLS, urls);
                intent.setOperation(operation);
                startAbility(intent);
            }
        });
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (mHandlerThread != null) {
            mHandlerThread.quit();
        }
    }
}